function setup_grid()
{
    var offsW = 30;
    var offsH = 95;

    var w = $('#content-log').innerWidth() - offsW;
    var h = $('#content-log').innerHeight() - offsH;

    $('#list').setGridHeight(h);
    $('#list').setGridWidth(w, true);

}

function get_setup_url()
{
    var cont = $('select[name=cont-sel] option:selected').val();
    var sys = $('input[name=system]:checked').val();
    var filt = $('input[name=filt]:checked').val();

    var param = "";

    if (cont > 0)
    {
        param = "cid="+cont+"&";
    }

    return 'req/log_list.php?' + param + 'type='+sys+'&imp='+filt;
}

function setup_url()
{

    $("#list").jqGrid('setGridParam', { 
                                       url: get_setup_url(),
                                       page:1
                                      }).trigger("reloadGrid");
}

function datetime_form(_value, _options, _row)
{
    var date;

    if (_value == '?')
    {
        return '?';
    }
    else
    {
        try
        {
            date = localeDateTime(mysqlTimeStampToDate(_value));
        }
        catch(e)
        {
            date = '&mdash;'
        }
    }

    return date;
}

function datetime_form_cvit(_value, _options, _row)
{
    var date;

    if (_value == null)
   	{
    	return '';
   	}
    if (_value == '-')
    {
        return '&mdash;';
    }
    else
    {
        try
        {
            date = localeDateTime(mysqlTimeStampToDate(_value));
        }
        catch(e)
        {
            date = '&mdash;';
        }
    }

    return date;
}


$(function() { 

  $.ajaxSetup({async: false});

  $.get('req/con_opt.php?type=opt', 
     function(data)
                {
                    $('select[name=cont-sel]').html('<option value="-1">- Все контроллеры -</option>\n' + data + "\n</select>");

                    if (typeof cid != 'undefined')
                    {
                        $('select[name=cont-sel] [value='+cid+']').attr("selected", "selected");
                    }
                });

  var defaultColNames = ['Время','Контр','Оператор','Сообщение'];
  var defaultColModel = [ 
                        {name:'log_date', index:'log_date', width:150, fixed: true, formatter: datetime_form}, 
                        {name:'cont', index:'cont', width:50, fixed: true, sortable: false, align:'center'},
                        {name:'user', index: 'user', width:150, fixed: true, sortable: false, align:'center'},
                        {name:'log_msg', index: 'log_msg', sortable: false}
                      ];
  
  if (use_alarm)
  {
	  defaultColNames.push('Время квитирования', 'Квитирование');
	  defaultColModel.push({name:'conf_date', sortable: false, width:160, fixed: true, formatter: datetime_form_cvit, align:'center'}, {name:'conf_user', sortable: false, width:150, fixed: true, align:'center'});
  }

  $("#list").jqGrid({
    url:get_setup_url(),
    datatype: 'json',
    mtype: 'GET',
    colNames: defaultColNames,
    colModel: defaultColModel,
    rowNum:30,
    sortname: 'log_date',
    sortorder: 'desc',
    pager: $("#pager")
  });
  
  setup_grid();

  $(window).resize(setup_grid);

  $("#sys-radio" ).buttonset().change(setup_url);
  $("#filt-log" ).buttonset().change(setup_url);
  $('select[name=cont-sel]').change(setup_url);
  $("#back").button().click(function()
  {
    if (typeof back_url != "undefined")
    {
        location.href=back_url;
    }
  });

  setup_url();    

  $.ajaxSetup({async: false});
  
  $.post('req/dat_edit.php',{oper:'load'},function(data)
  {
    var dat = $.parseJSON(data);
                
    $('#header-mon #title').html((typeof(dat.title) == 'undefined')? '' : dat['title']);
  });

        
}); 
